package com.cans.ssoserver.web.pms;

import com.cans.base.dto.PageRes;
import com.cans.base.dto.ResDto;
import com.cans.base.model.SsoUser;
import com.cans.ssoserver.service.user.api.ISsoUserService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * description: 系统接入WEB层
 *
 * @author cans
 * @date 2018/7/30
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private ISsoUserService userService;

    /**
     * 去分页列表
     * @return
     */
    @GetMapping("/toPageList")
    public String toPageList(){

        return "user/userList";
    }

    /**
     * 去添加
     * @return
     */
    @GetMapping("/toAdd")
    public String toAdd(){

        return "userUpdate";
    }

    /**
     * 去修改
     * @return
     */
    @GetMapping("/toUpdate")
    public String toUpdate(){

        return "user/userUpdate";
    }

    /**
     * 去查看
     * @return
     */
    @GetMapping("/view")
    public String view(){

        return "user/userView";
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/pageList")
    @ResponseBody
    public PageRes pageList(Integer page, Integer limit){

        Sort sort = new Sort(Sort.Direction.DESC,"createTime");
        Pageable pageable = new PageRequest(page - 1,limit,sort);

        return userService.findPage(pageable);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    @ResponseBody
    public ResDto delete(@PathVariable String id){

        userService.delete(id);

        return ResDto.success();
    }

    /**
     * 修改状态
     * @param id
     * @return
     */
    @PostMapping(value = "/disable")
    @ResponseBody
    public ResDto disable(String id){

        SsoUser user = userService.findOne(id);
        if(user.getEnable() == 1){
            user.setEnable(2);
        }else {
            user.setEnable(1);
        }
        userService.update(user);

        return ResDto.success();
    }
}
